# Step 1: replace null in creator
function Try-Guess-Creator-From-h([string]$h) {
    # 🦜 : it also can be Caesar
    if ($h.Contains('Caesar')){
        return 'Caesar'
    }

    $h1 = $h -match 'token-([^;\s]+)'
    if (-not $h1){
        # throw "malformed h, token not found :$h"
        # 🦜 : let's just return not found
        return '<404>';
    }
    $Matches.1
}

function Get3c-Stats{
    param (
        [string]$JsonlFile
    )

    # 1. get all objects
    $a = @()
    foreach($line in Get-Content $JsonlFile) {
        $obj = $line | ConvertFrom-Json
        $a += $obj
    }
    # 2. fix the creater
    # 2.1 group according to cid
    $g = $a | Group-Object -Property cid
    # 2.2 find a creator for each cid:
    $g | ForEach-Object {
        # Write-Host "Processing group: $_.Name"
        $cid = $_.Name
        $L = $_.Group
        $c0 = ''
        # 2.2.1 find the first creator that is not null
        for ($i = 0; $i -lt $L.Count; $i++) {
            $x = $L[$i]
            if ($x.creator.StartsWith('token-')){
                $c0 = $x.creator
            }elseif ($x.creator.StartsWith('Caesar')){
                $c0 = 'token-' + $x.creator
            } else{
                $c0 = Try-Guess-Creator-From-h($x.h)
            }
            if ($c0 -ne $null -and $c0 -ne '') {
                break
            }
        }
        # 2.2.3 set the creator for all objects in this group
        Write-Host "Found creator: $c0 for cid: $cid"
        for ($i = 0; $i -lt $a.Count; $i++) {
            $x = $a[$i]
            if ($x.cid -eq $cid){
                $x.creator = $c0
            }
        }
    }

    # 3: the filter them: TestScript or TestCase and it's not deleted
    $b = $a | Where-Object { $_.klass -eq 'TestScript' -or $_.klass -eq 'TestCase'} |
      Where-Object { $_.h -notmatch 'deleted' } |
      Where-Object { $_.creator -ne 'token-Caesar'} 


    # 4. make the stats object
    $b = $b | ForEach-Object {
        # 3.1 get h
        $h = $_.h

        # --------------------------------------------------
        # 3.2 get four: 01-k0: birth_type, 02-k1: pass_type, 03-t0. birth_time, 04-t1: pass_time, c: creator
        $h1 = $h -match ';(generated|created) ([^;]+)'
        if (-NOT $h1){
            Write-Error "⚠️ malformed h, created|generated flag not found :$h, $_"
            continue
        }
        $k0 = $Matches.1                #01. ✅️
        $s = $Matches.2
        # 🦜 : now we can just split with ' '
        $s = $s.split(" ")
        $t0 = Get-Date -Date $s[0]      #03. ✅️
        # 3. get the greened flag and time
        $h1 = $h -match ';(man_greened|auto_greened) ([^;]+)'
        if (-NOT $h1){
            throw "malformed h, green flag not found :$h"
        }
        $k1 = $Matches.1                #02. ✅️
        $s = $Matches.2
        $s = $s.split(" ")
        $t1 = Get-Date -Date $s[0] -AsUTC  #04. ✅️

        # --------------------------------------------------
        # 3.3 make the stats object
        $c = $_.creator.substring(6) # remove prefix 'token-'
        [pscustomobject]@{
            creator = $c; klass = $_.klass;
            birth_type = $k0; pass_type = $k1;
            birth_time = $t0; pass_time = $t1;
        }
    }
    # 5. return the object
    $b
}

# 1. read hi.jsonl into an array
$p = '~\Desktop\hi.jsonl'
$b = Get3c-Stats -JsonlFile $p
$b | Format-Table               # print
# 2. group by birth_time:
$b | Group-Object -Property {$_.birth_time.Date},creator -NoElement



# Step 4. write it to csv
$b | Export-Csv -Path './hi.csv' -NoTypeInformation
